package org.example;

public class Test2 {
    //leetcode 617 合并二叉树 https://leetcode.cn/problems/merge-two-binary-trees/description/
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) return null;
        int sum = 0;
        if (root1 != null) sum += root1.val;
        if (root2 != null) sum += root2.val;
        TreeNode root = new TreeNode(sum);
        TreeNode root1R = root1 == null ? null : root1.right;
        TreeNode root1L = root1 == null ? null : root1.left;
        TreeNode root2R = root2 == null ? null : root2.right;
        TreeNode root2L = root2 == null ? null : root2.left;
        root.right = mergeTrees(root1R, root2R);
        root.left = mergeTrees(root1L,root2L);
        return root;
    }
}
